package com.ruoyi.web.controller.greatWall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.LocalUrlConfig;
import com.ruoyi.system.domain.util.ReplacementUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.config.ExceptionThrower;
import com.ruoyi.framework.enums.ErrorInfo;
import com.ruoyi.system.domain.greatWall.CultureCategory;
import com.ruoyi.system.domain.greatWall.GwCulture;
import com.ruoyi.system.domain.greatWall.Pql;
import com.ruoyi.system.enums.CategoryNameEnum;
import com.ruoyi.system.service.CultureService;
import com.ruoyi.system.service.ISysDictDataService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * @author zxy
 * @date 2022/2/11 17:23
 * 长城文化
 */
@RestController
@RequestMapping(value = "/culture")
public class CultureController extends BaseController {


    @Autowired
    private CultureService cultureService;
    @Autowired
    private ISysDictDataService valueService;
    @Autowired
    private LocalUrlConfig localUrlConfig;
    @Autowired
    private ReplacementUtil replacementUtil;

    @PutMapping
    public GwCulture save(@RequestBody GwCulture culture){
        QueryWrapper<GwCulture> wrapper=new QueryWrapper<>();

        //判断部门是否存在
        LoginUser user = SecurityUtils.getCurrentUser();
        if (StringUtils.isEmpty(culture.getDepId())){
            culture.setDepId(String.valueOf(user.getDeptId()));
            wrapper.eq("dep_id",user.getDeptId());
        }else {
            wrapper.eq("dep_id",culture.getDepId());
        }
        List<Object> list;
        wrapper.eq("title",culture.getTitle());
        if (StringUtils.isEmpty(culture.getCreateDate())){
            culture.setCreateDate(DateUtil.getNowTime());
        }else {
            culture.setUpdateDate(DateUtil.getNowTime());
        }
        if (culture.getStatus() ==null){
            culture.setStatus(0);
        }

        if (StringUtils.isNotEmpty(culture.getId())){
            wrapper.ne("id",culture.getId());
            list= cultureService.listObjs(wrapper);
        }else {
            list=cultureService.listObjs(wrapper);
        }

/*        if (culture.getSlideshow() !=null && culture.getReplacement() !=null){
            List<String> timePeriods = replacementUtil.getTimePeriods(culture.getSlideshow(), culture.getReplacement());
            culture.setTimePeriods(timePeriods);
            culture.setSlideshow(timePeriods);
        }*/


        boolean flag = list.size() ==0;
        if (flag){
            cultureService.saveOrUpdate(culture);
        }else {
            ExceptionThrower.of(ErrorInfo.TITLE_EXIST);
        }



        cultureService.saveOrUpdate(culture);

        return culture;
    }

    @DeleteMapping(value = "/{id}")
    public void delete(@PathVariable String id){
        cultureService.removeById(id);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable String id) {
        GwCulture gwCulture = cultureService.getById(id);


        if (gwCulture !=null){
            if (StringUtils.isNotEmpty(gwCulture.getCategoryId())){
                String categoryName = CategoryNameEnum.getName(gwCulture.getCategoryId());
                gwCulture.setCategoryName(categoryName);
            }
            gwCulture = GwCulture.getCulture(gwCulture);
        }

        if(StringUtils.isNotEmpty(gwCulture.getStelaClass())){
            String stelaName = getStelaName(gwCulture.getStelaClass());
            gwCulture.setStelaName(stelaName);
        }


        List<String> stringList=new ArrayList<>();

        if(gwCulture.getSlideshow() !=null){

            if (gwCulture.getTimePeriods() ==null){

                String[] split = gwCulture.getSlideshow().split(",");

                for (int i = 0; i < split.length; i++) {
                    String s = localUrlConfig.getLocalUrl()+split[i];
                    stringList.add(s);

                }
                gwCulture.setTimePeriods(stringList);

            }else {
                List<String> str=new ArrayList<>();
                for (int i = 0; i < gwCulture.getTimePeriods().size(); i++) {
                    String s = localUrlConfig.getLocalUrl()+gwCulture.getTimePeriods().get(i);
                    str.add(s);
                }
                gwCulture.setTimePeriods(str);
            }

        }



        return success(gwCulture);
    };

    @GetMapping(value = "/list")
    public TableDataInfo page(GwCulture culture){
        startPage();
        return getDataTable(cultureService.pageSearch(culture));
    }

    @GetMapping(value = "/sortList")
    public AjaxResult list(){
        return success(Pql.sortList());
    }


    @GetMapping(value = "/category")
    public AjaxResult categoryList(){
        List<CultureCategory> categoryList =new LinkedList<>();
        List<String> list= Arrays.asList("历史典故","一览非遗","人物传奇","长城碑刻","纸上谈兵","长城诗歌");
        for (int i = 0; i < list.size(); i++) {
            CultureCategory category=new CultureCategory();
            Integer num=i+1;
            category.setId(num.toString());
            category.setName(list.get(i));
            categoryList.add(category);
        }
        return success(categoryList);
    }

    @GetMapping(value = "/stelaClass")
    public AjaxResult stela(){
        QueryWrapper<SysDictData> wrapper=new QueryWrapper<>();
        wrapper.eq("dict_type_id","3a479546b3c6fea20bb6fd0251ea8efa");
        List<CultureCategory> categoryList =new LinkedList<>();
        List<SysDictData> list = valueService.list(wrapper);
        for (int i = 0; i < list.size(); i++) {
            CultureCategory category=new CultureCategory();
            category.setId(list.get(i).getDictValue());
            category.setName(list.get(i).getDictLabel());
            categoryList.add(category);
        }
        return success(categoryList);
    }

    @GetMapping(value = "/stelaList")
    public List<SysDictData> stelaList(){
        QueryWrapper<SysDictData> wrapper=new QueryWrapper<>();
        wrapper.eq("dict_type_id","3a479546b3c6fea20bb6fd0251ea8efa");
        List<SysDictData> list = valueService.list(wrapper);
        return list;
    }

    @GetMapping(value = "/stelaClassPage")
    public Page<GwCulture> pageGwCulture(Page<GwCulture> page,GwCulture gwCulture){
        return cultureService.pageGwCulture(page,gwCulture);
    }


    @GetMapping(value = "review/{id}/{status}")
    public void status(@PathVariable String id,@PathVariable Integer status){
        cultureService.setStatus(id,status);
    }





    public  String getStelaName(String stelaId){
        QueryWrapper<SysDictData> wrapper=new QueryWrapper<>();
        wrapper.eq("dict_type_id","3a479546b3c6fea20bb6fd0251ea8efa");
        wrapper.eq("value",stelaId);
        SysDictData dictValue = valueService.getOne(wrapper);
        return dictValue.getDictLabel();
    }


}
